<?php
/**
 * Ecga控制器
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    v.k <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   SVN: $Id$
 */
namespace Project\Apps\Ecga\Controllers\Abstraction;


use Framework\System\Helper\HttpRequest;

use \Framework\Controller\HttpController;
use \Framework\View\Page;
use \Framework\Application\HttpApplication;
use \Framework\System\Helper\HttpResponse;
use \Project\Apps\Ecga\Library\Visitor;
use \Framework\Mapper\Query;
use Framework\View\Controls\DataList;

/**
 * Ecga控制器
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    v.k <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: <0.1>
 **/
class EcgaController extends HttpController
{
    //把 Visitor::getInstance()  复制给 $visitor 
    protected static $visitor;
    
    /**
     * 创建页面
     *
     * @return Page
     */
    protected static function createPage()
    {
        static $page = null;
        if (is_null($page)) {
            $page = new Page;
            $app = HttpApplication::getInstance();
            $page->templateDir = $app->getPath() . '/templates';
            $page->addScriptFile('/statics/js/storage/storage.js');
            $page->addScriptFile('/project/apps/ecga/js/utilities.js');
            $page->addScriptFile('/project/apps/ecga/js/backend.js');
            $sess = $app->createSession();
            if (isset($sess->message)) {
                isset($sess->message['warning'])   && $page->assign('warning', $sess->message['warning']);
                isset($sess->message['notice'])    && $page->assign('notice', $sess->message['notice']);
                unset($sess->message);
            }
        }

        return $page;
    }

    /**
     * 增加一条提示信息，
     * 如果第三个参数不为空的话则跳转到该地址
     *
     * @param string $msg  消息内容
     * @param string $type 消息类型，只能是warning和notice
     * @param string $url  跳转的URL地址
     *
     * @return void
     * @author Weber Liu
     */
    public static function message($msg, $type = 'notice', $url = '')
    {
        $sess = HttpApplication::getInstance()->createSession();
        $sess->message[$type][] = l($msg);
        if (!empty($url)) {
            HttpResponse::redirect($url);
        }
    }

    /**
     * 验证权限
     *
     * @param string $act 操作
     *
     * @return bool
     * @author Scott Ye
     */
    public static function validate($act)
    {
        self::$visitor = $visitor = Visitor::getInstance();

        if (!$visitor->logged()) {
            $href = '/profile.php?a=login';
            echo "<script type=\"text/javascript\">window.top.location.href='" . $href . "';</script>";

            return false;
        }

        return self::validatePrivilege($act);
    }

    /**
     * 验证用户权限
     *
     * @param string $act 操作
     *
     * @return bool
     * @author sunfengming
     */
    protected static function validatePrivilege($act)
    {
        $request = HttpRequest::getInstance();
        $table = $request->get('table', 'string', null);
        $table = empty($table) ? '' : '/' . $table;
        if (!Visitor::getInstance()->hasPrivilege(self::_getController() . $table, $act)) {
            $page = self::createPage();
            $page->setTitle('您没有权限进行此操作!');
            HttpResponse::output($page->render('nopermission'));
            return false;
        }

        return true;
    }

    /**
     * 获取控制器名称（小写）
     *
     * @return string
     * @author sunfengming
     */
    private static function _getController()
    {
        $calledClass = get_called_class();
        return strtolower(substr($calledClass, strrpos($calledClass, '\\') + 1));
    }

    /**
     * 创建DataList实例
     *
     * @param Query $query 查询集对象
     *
     * @return DataList
     * @author sunfengming
     */
    public static function createDataList(Query $query)
    {
        $dataList = new DataList($query);
        return $dataList->setPageSize(20);
    }
    
}


